import requests
from urllib.parse import quote

from bs4 import BeautifulSoup
import mysql.connector

def pa(p):
    #第几页
    print(p)
    #学校名称
    schoolName = "西北工业大学"

    #爬取链接
    url = "https://job.nwpu.edu.cn/jobInfoList.do?page="+p+"&order=infoPlus.submitTime&sort=desc&filter=%7bstatus%3a1%2cworkType%3a0%7d"

    #请求头
    headers = {
	    "Host": "job.nwpu.edu.cn",
	    "Connection": "keep-alive",
	    "Cache-Control": "max-age=0",
	    "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"96\", \"Microsoft Edge\";v=\"96\"",
	    "sec-ch-ua-mobile": "?0",
	    "sec-ch-ua-platform": "\"Windows\"",
	    "Upgrade-Insecure-Requests": "1",
	    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43",
	    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
	    "Sec-Fetch-Site": "none",
	    "Sec-Fetch-Mode": "navigate",
	    "Sec-Fetch-User": "?1",
	    "Sec-Fetch-Dest": "document",
	    "Accept-Encoding": "gzip, deflate, br",
	    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
	    "Cookie": "name=value; __jsluid_s=89e07d2562c01b7ced8043d433cd057b; JSESSIONID=C647579CED53A1D023C22CE74AA48425"
    }
    #爬取数据并解析
    html = requests.get(url=url, headers=headers)
    html.encoding = "utf-8"

    soup = BeautifulSoup(html.text, 'lxml')

    #找到所有<div class="col-md-12">标签
    dataList = soup.find_all("div",class_="col-md-12")

    for data in dataList:
        createTime = data.find("ul",class_="list-inline blog-tag-data").text.strip().split(" ")[0].split("发表于：")[1].strip()
        jobTitle = data.find("a").text.strip()
        print(createTime+"|"+jobTitle)
        mysql_conn(jobTitle,schoolName,createTime)

#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    for i in range(310,3058):
        pa(str(i))
